# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=graphite2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3.11
pkgrel=1
pkgdesc="Font rendering capabilities for complex non-Roman writing systems (mingw-w64)"
arch=('any')
url="https://github.com/silnrsi/graphite"
license=('LGPL2.1+')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
options=('strip' 'staticlibs')
source=("https://github.com/silnrsi/graphite/releases/download/${pkgver}/${_realname}-${pkgver}.tgz"
        "001-graphite2-1.3.8-win64.patch"
        "002-graphite2-1.2.1-pkgconfig.patch"
        "003-graphite2-1.3.9-staticbuild.patch"
        "004-graphite2-1.3.8-dllimport-fix.patch"
)
sha256sums=('bab92ed1844d6538e7e5bda76f6ac9aaf633e38b683983b942c78c8ce063ad7c'
            'bd6e108929110fcbfd69434221974395a4839710432fe58a9aa432e95bdf4354'
            '22bef0d90c0266b3b0eff01859c1e002f84c165d12242c2bfc153f8f98fb272a'
            '40cec4873e60027cc2ca464cf9731ef55de329a657170bfc5c98a92a387d644f'
            'c88c84a939117c277eb2e2337889890394018009fffd1318e44c8a3312ab8bef')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  apply_patch_with_msg 001-graphite2-1.3.8-win64.patch \
    002-graphite2-1.2.1-pkgconfig.patch \
    003-graphite2-1.3.9-staticbuild.patch \
    004-graphite2-1.3.8-dllimport-fix.patch
# python2 fixes
  sed -i "s:\/usr\/bin\/python:\/usr\/bin\/python2:" tests/{corrupt.py,defuzz,fnttxtrender,fuzzbidi,fuzztest,hbspeeds,jsoncmp}
}

check() {
  cd "${srcdir}"/build-${CARCH}
  # python2 fixes
  sed -i "s:python:python2:g" tests/CTestTestfile.cmake
  # we also want to use mingw-w64-x86_64 and mingw-w64-i686 versions of python
  mkdir -p "${srcdir}"/${_realname}-${pkgver}/tests/backup
  cp "${srcdir}"/${_realname}-${pkgver}/tests/{corrupt.py,defuzz,fnttxtrender,fuzzbidi,fuzztest,hbspeeds,jsoncmp} \
   ${srcdir}"/${_realname}-${pkgver}/tests/backup"
  sed -i "s:\/usr\/bin\/python2:${MINGW_PREFIX}\/bin\/python2:" \
   "${srcdir}"/${_realname}-${pkgver}/tests/{corrupt.py,defuzz,fnttxtrender,fuzzbidi,fuzztest,hbspeeds,jsoncmp}
  ctest || true
  rm "${srcdir}"/${_realname}-${pkgver}/tests/{corrupt.py,defuzz,fnttxtrender,fuzzbidi,fuzztest,hbspeeds,jsoncmp}
  cp "${srcdir}"/${_realname}-${pkgver}/tests/backup/* "${srcdir}"/${_realname}-${pkgver}/tests
  rm -rf "${srcdir}"/${_realname}-${pkgver}/tests/backup
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DGRAPHITE2_COMPARE_RENDERER=ON \
      ../${_realname}-${pkgver}

  make
}


package() {
  cd "${srcdir}"/build-${CARCH}
  make DESTDIR=${pkgdir} install

  # License
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
